'use strict';

app.controller('InfocenterCtrl', 
['$scope','$http', 'dialogs', '$state', 'InfocenterService','$uibModal',
function($scope, $http, dialogs, $state, InfocenterService,$uibModal) {
	var vm = $scope.vm = this;
	var defaultQuery = {};
	vm.query = angular.copy(defaultQuery);
	
	vm.infolist = [{key:0,value:"未读"},{key:1,value:"已读"}];
	vm.flexigridObj = {};	
	vm.options = InfocenterService.getGridOptions();

	$scope.$watch('$viewContentLoaded', function(){
		var s = 1;
	})
	
	//初始化消息类型下拉框
	InfocenterService.queryMessageType().then(function(data){
		vm.messageTypes = data;
	});
	//查询
	vm.search = function(){
		var param = angular.copy(vm.query);
		delete param.infoType;
		vm.query.infoType && (param = angular.extend(param,eval('('+vm.query.infoType+')')));
		vm.flexigridObj.flexOptions({
			newp: 1,
			extParam: param
		}).flexReload();
	}

	// 重置
	vm.reset = function(){
		vm.query = angular.copy(defaultQuery);
		vm.search();
	}
	
	//详情
	vm.showDetail = function(index){
		vm.info = vm.flexigridObj.getRawRecords(index);
		$("#infoDetail").modal();
	}
	
	// 批量删除
	vm.batchDel = function(index){
		var selectData = vm.flexigridObj.getCheckedRows();
		var ids = [];
		angular.forEach(selectData,function(item){
			ids.push(item.infoId);
		});
		var param = {
				"infoIds":ids,
				"logInfo":"批量删除" + selectData.length + "条消息"
				};
		if (ISS.isNotEmpty(selectData)) {
			// 弹出确认删除提示信息
			var dlg = dialogs
					.confirm(getMessage("common.deleteConfirm"));
			dlg.result.then(function() {
				InfocenterService.remove(param).then(function(data) {
					// 刷新列表
					vm.flexigridObj.flexReload();
					//弹出提示信息
					dialogs.notify(data);
				});
			});
		} else {
			// 提示请选择一条或多条信息
			dialogs.notify(undefined,
					getMessage("common.plzSelectOneRecourd"));
		}
	}
	
	//查看消息详情
    vm.showInfoDetail = function(index){
    	var param = vm.flexigridObj.getRawRecords(index);
    	param.loginUser = $scope.app.loginUser;
    	var modalInstance = $uibModal.open({
    	     animation: true,
    	     templateUrl: ISS.getPath('app/modules/basic/infocenter/modals/infoDetail.html'),
    	     controller: 'InfoDetailCtrl',
    	     backdrop:'static',
    	     resolve: {
    	    	 param: function () {
    	         return param;
    	       }
    	     }
    	   });
    	modalInstance.result.then(function (result) {
    		$scope.$emit("ReadMessage", { description: "刷新表头消息" });
    		// 刷新列表
			vm.flexigridObj.flexReload();
        });
    }
    
    $scope.$on("RefreshGridFromHeader", function (event, data) {
    	vm.flexigridObj.flexReload();
    });
}]);
